package com.chepaandvasia.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import com.chepaandvasia.entities.Role;
import com.chepaandvasia.entities.User;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@Repository
public class UserDAOImpl implements UserDAO {

    @PersistenceContext
    private EntityManager entinyManager;

    @Override
    public void create(User user) {
        if (user == null || user.getId() != null) {
            throw new IllegalArgumentException(
                    "User already has some ID. We need only new users to store in database");
        }
        try {
            // find user in database
            getUserWithLogin(user.getLogin());
        } catch (UsernameNotFoundException ex) {
            // if user does not exists we should save him to database
            entinyManager.persist(user);
        }
    }

    @Override
    public void update(User user) {

        if (user == null || user.getId() == 0 || user.getId() < 0) {
            throw new IllegalArgumentException();
        }
        entinyManager.merge(user);
    }

    @Override
    public void remove(User user) {
        if (user == null || user.getId() == 0 || user.getId() < 0) {
            throw new IllegalArgumentException();
        }
        User v = entinyManager.find(User.class, user.getId());
        entinyManager.remove(v);
    }

    @Override
    public User get(Long id) {
        if (id == null || id < 0) {
            throw new IllegalArgumentException();
        }
        User user = entinyManager.find(User.class, id);
        return user;
    }

    @Override
    public List<User> findAllUsersForRole(Role position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<User> findAllUsers() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public UserDetails getUserWithLogin(String login) throws UsernameNotFoundException {
        TypedQuery<User> query = entinyManager.createQuery("SELECT u FROM u WHERE u.login=:login", User.class);
        query.setParameter("login", login);
        User result = null;
        try {
            result = query.getSingleResult();
        } catch (NoResultException ex) {
            // if there is NO user in database just throw exception
            throw new UsernameNotFoundException("User with login " + login + " does not exists");
        }
        // if everything is OK and user is found just return user
        return result;
    }

    public void setEntinyManager(EntityManager entinyManager) {
        this.entinyManager = entinyManager;
    }
}
